package com.hanbang.mrcm.home;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.adapter.BannerAdAdapter;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.bean.BannerAdData;
import com.hanbang.mrcm.bean.MerchantData;
import com.hanbang.mrcm.myview.ListViewForScrollView;
import com.hanbang.mrcm.myview.ScaleImageView;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.MyToast;
import com.hanbang.mrcm.utils.PagingHelp;
import com.hanbang.mrcm.utils.SystemTools;
import com.hanbang.mrcm.utils.UiUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/*
 * 疯狂点击
 */
public class InsaneClickActivity extends BaseActivity {
	private PullToRefreshScrollView scrollView = null;
	private ArrayList<BannerAdData> bannerAdDatas = new ArrayList<BannerAdData>();
	private BannerAdAdapter bannerAdAdapter = null;
	private ListViewForScrollView listview = null;
	private MyAdapter adapter = null;
	private ArrayList<MerchantData> listDatas = new ArrayList<MerchantData>();

	private PagingHelp pagingHelp = new PagingHelp();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.insane_click);
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img_5_2);
		initView();
		setOnClickListener();
		getTopData();
		getCrazyClick();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.setClass(InsaneClickActivity.this,
						GoodListActivity.class);
				intent.putExtra("trade_id", listDatas.get(position)
						.getUser_name());
				startActivity(intent);
			}
		});

		scrollView.setOnRefreshListener(new OnRefreshListener2<ScrollView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ScrollView> refreshView) {
				listDatas.clear();
				pagingHelp.clear();
				bannerAdDatas.clear();
				getTopData();
				getCrazyClick();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ScrollView> refreshView) {

			}
		});

	}

	@SuppressWarnings("unused")
	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		setTitle(findViewById(R.id.top_bar_title), "疯狂点击");
		scrollView = (PullToRefreshScrollView) findViewById(R.id.scrollview);
		listview = (ListViewForScrollView) findViewById(R.id.listview);
		scrollView.setMode(Mode.PULL_FROM_START);
		adapter = new MyAdapter();
		listview.setAdapter(adapter);
		listview.setFocusable(false);
		bannerAdAdapter = new BannerAdAdapter(this,
				(RelativeLayout) findViewById(R.id.banner_ad), bannerAdDatas);
		bannerAdAdapter.setWidthAndHeight(16, 9);
		bannerAdAdapter.notifyDataSetChanged();
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return listDatas.get(position).getActivity_id();
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MerchantData data = (MerchantData) getItem(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.insane_click_list_item, null);
				holder = new ViewHolder();
				holder.imageView = (ScaleImageView) convertView
						.findViewById(R.id.imageview);
				UiUtils.applyFont(convertView);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			bitmapUtils.display(holder.imageView,
					SystemTools.getHttpFileUrl(data.getImg_url()));
			return convertView;
		}

		private class ViewHolder {
			ScaleImageView imageView = null;
		}
	}

	/*
	 * http://192.168.0.88/tools/good_list.ashx?action=crazyclick&channel_id=9&
	 * category_id=9&pageindex=1&pagesize=10 --疯狂点击
	 */
	private void getCrazyClick() {
		httpUtils.send(HttpMethod.GET, HttpInterfaces.crazyclick
				+ "&channel_id=" + getIntent().getIntExtra("channel_id", -1)
				+ "&category_id=" + getIntent().getIntExtra("category_id", -1)
				+ "&pageindex=" + pagingHelp.getPageindex() + "&pagesize="
				+ pagingHelp.getPageCount(), new MyRequestCallBack<String>(
				this, getString(R.string.loadding)) {
			@Override
			public void onFailure(HttpException error, String msg) {
				super.onFailure(error, msg);
				scrollView.onRefreshComplete();
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				super.onSuccess(responseInfo);
				try {
					JSONObject json = new JSONObject(responseInfo.result);
					int status = json.getInt("status");
					if (status == 1) {
						JSONArray result = json.getJSONArray("result");
						Collection<MerchantData> c = JsonHelper
								.parseCollection(result, ArrayList.class,
										MerchantData.class);
						listDatas.addAll((Collection<MerchantData>) (pagingHelp
								.getValidData(InsaneClickActivity.this, c)));
						adapter.notifyDataSetChanged();
					} else if (status == -1) {
						MyToast.show(InsaneClickActivity.this,
								json.getString("msg"), Toast.LENGTH_SHORT);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				scrollView.onRefreshComplete();

			}
		});
	}

	private void getTopData() {

		httpUtils.send(HttpMethod.GET, HttpInterfaces.crazyPic + "&channel_id="
				+ getIntent().getIntExtra("channel_id", -1) + "&category_id="
				+ getIntent().getIntExtra("category_id", -1),
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								JSONArray result = json.getJSONArray("result");
								Collection<BannerAdData> c = JsonHelper
										.parseCollection(result,
												ArrayList.class,
												BannerAdData.class);
								bannerAdDatas.addAll(c);
								bannerAdAdapter.notifyDataSetChanged();
								scrollView.onRefreshComplete();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});

	}

	@Override
	protected void onResume() {
		super.onResume();
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img_5_2);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
